package com.wangjw.designPattern.singleton;

/**
 * Created by wangjw on 18/8/16.
 */
public class LazySafetySingleton {

    private static LazySafetySingleton instance;

    private LazySafetySingleton() {

    }

    // 方法中声明synchronized关键字
    public static synchronized LazySafetySingleton getInstance() {
        if (instance == null) {
            instance = new LazySafetySingleton();
        }
        return instance;
    }

    // 同步代码块实现
    public static LazySafetySingleton getInstance1() {
        synchronized (LazySafetySingleton.class) {
            if (instance == null) { // 懒汉式
                instance = new LazySafetySingleton();
            }
        }
        return instance;
    }

    public static void createString() {
        System.out.println("create String");
    }

    public static void main(String[] args) {
        LazySafetySingleton.createString();
    }
}
